package net.huashitong.user.web;

import jodd.util.StringUtil;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Department;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.filter.DepartmentFilter;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.DepartmentService;
import net.huashitong.zhApp.controller.BaseCtrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门接口
 */
@Controller
@RequestMapping("/api/department")
public class DepartmentController extends BaseCtrl {

    @Autowired
    private DepartmentService service;
    @Autowired
    private BnpUserService bnpUserService;


    @ResponseBody
    @RequestMapping(value = "/dealWithDept", method = RequestMethod.GET)
    public Map<String, Object> dealWithDept() {
        List<Department> departments = new ArrayList<>();
        //查询所有用户
        List<BnpUser> users = bnpUserService.query();
        //查询用户表中的所有部门
        BnpUserFilter bnpUserFilter = new BnpUserFilter();
        List<BnpUser> deptList = bnpUserService.queryBranchsByType(bnpUserFilter);
        Map<String, BnpUser> deptMap = new HashMap<>();

        for (BnpUser bnpUser : users) {
            if (!deptMap.containsKey(bnpUser.getBranch())) {
                deptMap.put(bnpUser.getBranch(), bnpUser);

                Department department = new Department();
                String branchType = bnpUser.getBranchType();
                String branch = bnpUser.getBranch();
                switch (branchType) {
                    case "0":
                        department.setDeptType("0");
                        department.setTypeName("责任单位");
                        department.setDeptName(branch);
                        break;
                    case "1":
                        department.setDeptType("1");
                        department.setTypeName("审批部门");
                        department.setDeptName(branch);
                        break;
                    case "2":
                        department.setDeptType("2");
                        department.setTypeName("其他");
                        department.setDeptName(branch);
                        break;
                    case "4":
                        if (StringUtil.isNotBlank(bnpUser.getGroupCode())) {
                            department.setDeptType("4");
                            department.setTypeName("督查组");
                            department.setDeptName(branch);
                        } else {
                            department.setDeptType("0");
                            department.setTypeName("责任单位");
                            department.setDeptName(branch);
                            break;
                        }
                        break;
                    case "6":
                        department.setDeptType("2");
                        department.setTypeName("其他");
                        department.setDeptName(branch);
                        break;
                    case "7":
                        department.setDeptType("2");
                        department.setTypeName("其他");
                        department.setDeptName(branch);
                        break;
                    default:
                        break;
                }
                departments.add(department);
            }
        }
        service.batchSave(departments);
        return coverMessage("200", "成功");
    }


    /**
     * 查询
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryByFilter", method = RequestMethod.POST)
    public Map<String, Object> queryByFilter(@RequestBody DepartmentFilter filter) {
        List<Department> list = service.queryByFilter(filter);
        return coverMessage("200", "查询成功", list);
    }

    /**
     * 查询不分页
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryByFilterNoPage", method = RequestMethod.POST)
    public Map<String, Object> queryByFilterNoPage(@RequestBody DepartmentFilter filter) {
        List<Department> list = service.queryByFilterNoPage(filter);
        return coverMessage("200", "查询成功", list);
    }

    /**
     * 添加
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
    public Map<String, Object> addDepartment(@RequestBody DepartmentFilter filter) {
        service.addDepartment(filter);
        return coverMessage("200", "添加成功");
    }

    /**
     * 修改
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDepartment", method = RequestMethod.POST)
    public Map<String, Object> editDepartment(@RequestBody Department newDept) throws NoSuchFieldException {
        Department oldDept = service.getById(newDept.getSysId());
        service.updateDepartment(oldDept, newDept);
        return coverMessage("200", "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Map<String, Object> deleteDepartment(@PathVariable List<String> id) {
        service.batchDelete(id);
        return coverMessage("200", "删除成功");
    }

    /**
     * 查询单位名称是否已存在
     *
     * @param deptName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDeptNameExist", method = RequestMethod.GET)
    public Map<String, Object> queryDeptNameExist(@RequestParam String deptName) {
        boolean result = service.queryDeptNameExist(deptName);
        if (result) {
            return coverMessage("1", "该单位已存在");
        } else {
            return coverMessage("0", "该单位不存在");
        }
    }
}
